package com.team9.fitness.controller;

import com.team9.fitness.entity.ExerciseInfo;
import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 运动信息控制器
 */
@RestController
@RequestMapping("/api/exercise-info")
@Slf4j
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class ExerciseInfoController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 创建运动信息
     */
    @PostMapping
    public CompletableFuture<ResponseEntity<Map<String, Object>>> createExerciseInfo(
            @RequestBody ExerciseInfo exerciseInfo) {

        log.info("创建运动信息请求: name={}, category={}", exerciseInfo.getName(), exerciseInfo.getCategory());

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("exerciseInfo", exerciseInfo);

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_CREATE", "POST", "/api/exercise-info",
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "运动信息创建成功");
                        result.put("data", response.getData());
                        log.info("运动信息创建成功: name={}", exerciseInfo.getName());
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "运动信息创建失败");
                        log.warn("运动信息创建失败: name={}, error={}", exerciseInfo.getName(), response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("运动信息创建异常: name={}", exerciseInfo.getName(), throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "运动信息创建服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取所有运动信息
     */
    @GetMapping
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getAllExerciseInfo() {

        log.info("获取所有运动信息请求");

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_GET_ALL", "GET", "/api/exercise-info",
                null, new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("data", response.getData());
                        log.info("获取所有运动信息成功");
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取运动信息失败");
                        log.warn("获取所有运动信息失败: error={}", response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取所有运动信息异常", throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取运动信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 根据ID获取运动信息
     */
    @GetMapping("/{id}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getExerciseInfoById(@PathVariable Long id) {

        log.info("根据ID获取运动信息请求: id={}", id);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("id", id);

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_GET_BY_ID", "GET", "/api/exercise-info/" + id,
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("data", response.getData());
                        log.info("获取运动信息成功: id={}", id);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "运动信息不存在");
                        log.warn("获取运动信息失败: id={}, error={}", id, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取运动信息异常: id={}", id, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取运动信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 根据分类获取运动信息
     */
    @GetMapping("/category/{category}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getExerciseInfoByCategory(
            @PathVariable String category) {

        log.info("根据分类获取运动信息请求: category={}", category);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("category", category);

        return zrddsSendService
                .sendBusinessRequest("EXERCISE_INFO_GET_BY_CATEGORY", "GET", "/api/exercise-info/category/" + category,
                        null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("data", response.getData());
                        log.info("获取分类运动信息成功: category={}", category);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取分类运动信息失败");
                        log.warn("获取分类运动信息失败: category={}, error={}", category, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取分类运动信息异常: category={}", category, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取分类运动信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 搜索运动信息
     */
    @GetMapping("/search")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> searchExerciseInfo(@RequestParam String keyword) {

        log.info("搜索运动信息请求: keyword={}", keyword);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("keyword", keyword);

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_SEARCH", "GET", "/api/exercise-info/search",
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("data", response.getData());
                        log.info("搜索运动信息成功: keyword={}", keyword);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "搜索运动信息失败");
                        log.warn("搜索运动信息失败: keyword={}, error={}", keyword, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("搜索运动信息异常: keyword={}", keyword, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "搜索运动信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取所有运动分类
     */
    @GetMapping("/categories")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getAllCategories() {

        log.info("获取所有运动分类请求");

        return zrddsSendService
                .sendBusinessRequest("EXERCISE_INFO_GET_CATEGORIES", "GET", "/api/exercise-info/categories",
                        null, new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("data", response.getData());
                        log.info("获取所有运动分类成功");
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取运动分类失败");
                        log.warn("获取所有运动分类失败: error={}", response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取所有运动分类异常", throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取运动分类服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 更新运动信息
     */
    @PutMapping("/{id}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> updateExerciseInfo(@PathVariable Long id,
            @RequestBody ExerciseInfo exerciseInfo) {

        log.info("更新运动信息请求: id={}, name={}", id, exerciseInfo.getName());

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("id", id);
        requestBody.put("exerciseInfo", exerciseInfo);

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_UPDATE", "PUT", "/api/exercise-info/" + id,
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "运动信息更新成功");
                        result.put("data", response.getData());
                        log.info("运动信息更新成功: id={}", id);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "运动信息更新失败");
                        log.warn("运动信息更新失败: id={}, error={}", id, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("运动信息更新异常: id={}", id, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "运动信息更新服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 删除运动信息
     */
    @DeleteMapping("/{id}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> deleteExerciseInfo(@PathVariable Long id) {

        log.info("删除运动信息请求: id={}", id);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("id", id);

        return zrddsSendService.sendBusinessRequest("EXERCISE_INFO_DELETE", "DELETE", "/api/exercise-info/" + id,
                null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "运动信息删除成功");
                        log.info("运动信息删除成功: id={}", id);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "运动信息删除失败");
                        log.warn("运动信息删除失败: id={}, error={}", id, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("运动信息删除异常: id={}", id, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "运动信息删除服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
